const { Article } = require('../config/Sequelize')
const { CriteriaExample } = require('../utils/CriteriaExample')

/**
 * 查询文章
 * @param {*} article 
 * @returns 
 */
const selecArticleList = async (article = {}) => {
  const articleExample = new CriteriaExample(article)
  console.log(article)
  articleExample.and()
    .like('title')
    .eq('author')
    .eq('categoryId')
    .eq('status')
    .eq('original')
    .eq('top')
  // const deptExample = new CriteriaExample(article)
  const page = articleExample.getPage()
  console.log(page)
  const { count: total, rows: data } = await Article.findAndCountAll({
    where: articleExample.getWhere(),
    order: [['top', 'DESC'], ['updatedAt', 'DESC'], ['createdAt', 'DESC']],
    ...page
  })
  return { page: articleExample.page, pageSize: articleExample.pageSize, total: total, data }
}

/**
 * 根据文章id查询文章信息
 * @param {*} id 
 * @returns 
 */
const selectArticleById = async (id) => {
  console.log(id)
  const article = await Article.findOne({
    where: { articleId: id },
    // include: [{
    //   model: SysDept, required: false,
    // }]
  })
  return article
}


/**
 * 添加文章信息
 * @param {*} article 
 * @returns 
 */
const addArticle = async (article) => {
  return Article.create(article)
}

/**
 * 更新文章信息
 * @param {*} article 
 * @returns 
 */
const updateArticle = async (article) => {
  return Article.update(article, {
    where: {
      articleId: article.articleId
    }
  })
}

/**
 * 删除文章信息
 * @param {*} article 
 * @returns 
 */
const deleteArticle = async (id) => {
  return Article.destroy({
    where: { articleId: id }
  })
}

module.exports = {
  selecArticleList,
  addArticle,
  updateArticle,
  deleteArticle,
  selectArticleById
}